package cn.smbms.service;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.smbms.dao.UserMapper;
import cn.smbms.pojo.User;

@Service
public class UserService implements UserServiceI {
	@Resource
	private UserMapper userDao;

	/*
	 * 用户登录
	 * 
	 * @see cn.smbms.service.UserServiceI#login(java.lang.String,
	 * java.lang.String)
	 */
	@Override
	public User login(String userCode, String userPassword) {
		return userDao.login(userCode, userPassword);

	}

	/*
	 * 修改用户密码
	 * 
	 * @see cn.smbms.service.UserServiceI#updatePwd(java.lang.Integer,
	 * java.lang.String)
	 */
	@Override
	public int updatePwd(Integer id, String userPassword) {
		return userDao.updatePwd(id, userPassword);

	}

	@Override
	public List<User> getUserListByUserName(String name, Integer pageNo) {
		return userDao.getUserListByUserName(name, pageNo);
	}

	public int count(String name) {
		return userDao.count(name);
	}

	@Override
	public User getUserById(Integer id) {
		// TODO Auto-generated method stub
		return userDao.getUserById(id);
	}

	@Override
	public int updateUser(User user) {
		// TODO Auto-generated method stub
		return userDao.update2(user);
	}

	@Override
	public int addUser(User user) {
		// TODO Auto-generated method stub
		return userDao.add(user);
	}

	@Override
	public int deleteUser(Integer id) {
		// TODO Auto-generated method stub
		return userDao.deleteUserById(id);
	}

	@Override
	public boolean yanZhengOldPassword(Integer id, String password) {
		// TODO Auto-generated method stub
		return userDao.yanZhengOldPassword(id, password) == 1;
	}

	@Override
	public boolean yanZhengUserCodeExists(String userCode) {
		// TODO Auto-generated method stub
		return userDao.yanZhengUserCodeExists(userCode) > 0;
	}

	// 事务测试
	@Transactional
	public void deleteUserById(int id) {
		int r = userDao.deleteUserById(id);
		// int i = 1 / 0;
		// try {
		// int i = 1 / 0;
		// } catch (Exception e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// }

	}
}
